$(document).ready(function() {
    newsManage.init();

});
var _mmg = null;
var editIndex = undefined;
var editRow=undefined;
var endEditing=undefined;
var newsManage = function(){
    return{
        init: function () {
            _mmg = $('#dg').datagrid({
                url: '/home/newsPage',
                params: $("#dbConfigForm").serialize(),
                method: 'post',
                rownumbers: true,
                striped: true,
                nowrap: true,
                pageList: [25, 30, 35],
                idField: 'id', //主键
                onClickRow: 'onClickRow',
                columns: [
                    [
                        {field: 'ck', checkbox: true},
                        {field: 'id', title: 'id', hidden: true},
                        {field: 'Title', title: '标题', width: 400},
                        {field: 'Newstype', title: '新闻类型', width: 100},
                        {field: 'Author', title: '作者', width: 100},
                        {field: 'Createtime', title: '发布时间', width: 150},
                        {field: 'Modifytime', title: '最后修改时间', width: 150},
                        {
                             field: 'operation',
                             title: '操作',
                             width: 100,
                             type: 'String',
                             formatter: function (val, item, rowIndex) {

                                       // _mmg.datagrid("endEdit", editIndex);
                                      // return '<span><a href="#" onclick="dbConfig.save(\'' + item.id + ',' + item.star_time + ',' + item.repetition_period + '\')"><span style="color:blue;">保存</span></a></span>';
                                      return '<span><a href="#" onclick="newsManage.delete()"><span style="color:blue;">复制链接</span></a></span> &nbsp;   <span><a href="#" onclick="newsManage.recycle(\''+ item.Id +'\')"><span style="color:#ff1f11;">删除</span></a></span>';
                             }
                             },
                    ]
                ],


            }).datagrid("clientPaging");
        },
        //查询
        load: function () {
            var neswTitle= $("#neswTitle").val();
            var newsType= $("#type").combobox('getValue');
            var firstTime = $("#firsTtime").datetimebox('getValue');
            var lastTime = $("#lastTime").datetimebox('getValue');
            $('#dg').datagrid('load', {
                neswTitle: neswTitle,
                newsType: newsType,
                firstTime: firstTime,
                lastTime: lastTime

            });
        },

        //新增_弹出框
        addPopu: function () {
            $('#w').window('open');
        },
        //关闭弹出框
        closePopu: function () {
            $('#w').window('close');
        },
        //跳转到  回收站列表
        Recycle: function () {
            window.location.href = '/home/newsManageRecycle';
        },
        //保存操作
        save: function () {
            var Title= $("#Title").val();
            var Newstype= $("#NewsType").combobox('getValue');
            var Author= $("#Author").val();
            var Newscontent= $(document.getElementsByTagName('iframe')[0].contentWindow.document.body).html();
            $.ajax({
                type: 'POST',
                url: '/home/addnews',
                dataType: 'json',
                data: {Title:Title,Newstype:Newstype,Author:Author,Newscontent:Newscontent},
                success: function (result) {

                    if (result['info'] == 'Success') {
                       alert("添加新闻成功！")
                        newsManage.closePopu();
                        window.location.reload();

                    } else {
                        alert("添加新闻失败！")
                    }
                }
            })

        },

        //新闻加入回收站
        recycle: function (Id) {
            $.messager.confirm('确认', '确定要加入回收站？', function (r) {
                if (r) {
                        $.ajax({
                            type: 'POST',
                            url: '/home/recyclenewsbyid',
                            data: 'Id=' + Id,
                            success: function (result) {
                                if (result['info'] == '1') {
                                    alert("加入回收站成功！")
                                    window.location.reload();
                                    return;
                                } else {
                                    alert('加入回收站失败！');
                                    return;
                                }
                            }
                        })
                }
            });


        },
        //清空表单
        clearForm: function(formID){
            $("#" + formID).each(function () {
                // iterate the elements within the form
                $("#neswTitle").val('')
                $('#type').combobox('clear');
                $('#firstTime').datetimebox('clear');
                $('#lastTime').datetimebox('clear');
                $(':input', this).each(function () {
                    var type = this.type, tag = this.tagName.toLowerCase();
                    if (type == 'text' || type == 'password' || tag == 'textarea' || type == "file")
                        this.value = '';
                    else if (type == 'checkbox' || type == 'radio')
                        this.checked = false;
                    else if (tag == 'select')
                        $('#newsType').combobox('');

                });
            });
            $('#dg').datagrid('load', {});
        },

    }

}
();

